package 字符串;

public class LC_151翻转字符串里的单词 {
    public static void main(String[] args){
        String s="the sky is blue";
        System.out.println(reverseWords(s));
    }
    public static String reverseWords(String s) {
        s.trim();//用于删除字符串两端的空白字符
        int j=s.length()-1;
        int i=j;
        StringBuilder res=new StringBuilder();
        while(i>=0){
            while(i>=0 && s.charAt(i)!=' ')
                i--;
            //substring()索引左闭右开
            res.append(s.substring(i+1,j+1)+" ");
            while(i>=0 && s.charAt(i)==' ')
                i--;
            j=i;
        }
        return res.toString().trim();
    }
}
